package com.java.util;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

public class MyTools {

	public static String getUUID(){
		String uuid =  UUID.randomUUID().toString().replaceAll("-", "");
		return uuid;
	}
	
	public static boolean isEmpty(Object pObj) {
        if (pObj == null)
            return true;
        if (pObj == "" || pObj.equals("null"))
            return true;
        if (pObj instanceof String) {
            if (((String) pObj).length() == 0) {
                return true;
            }
        } else if (pObj instanceof Collection) {
            if (((Collection<?>) pObj).size() == 0) {
                return true;
            }
        } else if (pObj instanceof Map) {
            if (((Map<?, ?>) pObj).size() == 0) {
                return true;
            }
        }
        return false;
    }
	
	public static List<Integer> randomNumbers(int min, int max, int count) {
        Random random = new Random();
        Set<Integer> uniqueNumbers = new HashSet<>();
        while (uniqueNumbers.size() < count) {
            int randomNumber = random.nextInt(max - min + 1) + min;
            uniqueNumbers.add(randomNumber);
        }
        List<Integer> list = new ArrayList<>(uniqueNumbers);
        Collections.shuffle(list); 
        return list;
    }
	
	public static String getDateStr(Date date) {
		if(null == date) {
			date = new Date();
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
	}
	
	public static String getMD5(String input) {  
        try {  
            // 创建一个信息摘要实例  
            MessageDigest md = MessageDigest.getInstance("MD5");  
            // 将输入字符串转换为字节数组  
            byte[] messageDigest = md.digest(input.getBytes(StandardCharsets.UTF_8));  
            // 将字节数组转换为16进制字符串  
            BigInteger number = new BigInteger(1, messageDigest);  
            String hashtext = number.toString(16);  
            // 如果生成的字符串长度小于32，则在前面补0  
            while (hashtext.length() < 32) {  
                hashtext = "0" + hashtext;  
            }  
            return hashtext;  
        } catch (NoSuchAlgorithmException e) {  
            throw new RuntimeException(e);  
        }  
    }
	
	public static void main(String[] args) {
		System.out.println(getMD5("1"));
	}
}
